/*
 * DirectoryReader.h
 *
 *  Created on: Mar 26, 2010
 *      Author: Lukasz Spintzyk
 */

#ifndef DIRECTORYREADER_H_
#define DIRECTORYREADER_H_

#ifndef PH_H_
#include "ph.h"
#endif

#ifndef INODEREADER_H_
#include "InodeReader.h"
#endif

#ifndef DIRENTRY_H_
#include "DirEntry.h"
#endif

#ifndef INODE_H_
#include "Inode.h"
#endif

class InodeManager;
class BlockManager;
class DirEntry;
class Inode;
class SuperBlock;
class DirectoryIterator;

class DirectoryReader {
public:
	friend class DirectoryIterator;
	DirectoryReader(BlockManager& b, InodeManager& i);
	virtual ~DirectoryReader();


	/*
	 * \throw FSException
	 */
	virtual InodePtr path2Inode(const std::string& path) = 0;


	virtual void setUpRootDirectory(uint16_t uid, uint16_t gid) = 0;
	virtual void setUpDirectory(InodePtr inode, uint32_t parent_inode_num) = 0;

	virtual void addDirEntry(DirEntry& entry, InodePtr entry_inode) = 0;

	virtual boost::shared_ptr<DirectoryIterator> begin(InodePtr inode) = 0;


protected:
	virtual InodePtr relPath2Inode(
			const uint32_t dir_inode,
			const std::string& relative_path) = 0;



	InodeManager& d_inodeMgr;
	BlockManager& d_blockMgr;
	SuperBlock& d_sb;
};



class DirectoryIterator
{
public:
	DirectoryIterator(InodePtr i, DirectoryReader* dirReader);

	void operator++();
	DirEntry operator*();
	bool removeEntry();
	bool end();
	InodePtr d_inode;
	uint32_t d_pos;
	uint32_t d_prevPos;
	uint32_t d_block;
	DirectoryReader* d_dirReader;
	byte* d_buffer;
	uint32_t d_nodeNum;
	DirEntry d_entry;
};


#endif /* DIRECTORYREADER_H_ */
